<template>
  <div style="height: 100%">
    <!-- 背景图片 -->
    <div class="background">
      <img src="../assets/img/login/login_bg.jpg" width="100%" height="100%" />
    </div>
    <!-- 主体内容 -->
    <div class="bg_login">
      <div class="login_content">
        <div class="header">
          <!--图标logo-->
          <div class="login-header">
            <img src="../assets/img/login/login_logo.png" />
          </div>
          <img src="../assets/img/login/login_mode.png" class="login_img" />
        </div>
        <!--登录内容-->
        <div class="login-wrap">
          <div class="title_name">账号登录</div>
          <img src="../assets/img/login/logo.png" class="logo_img" alt="" />
          <!--账号密码登录-->
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginFormRules"
          >
            <el-form-item prop="username">
              <el-input
                type="text"
                v-model="loginForm.username"
                placeholder="请输入账号"
                prefix-icon="iconfont icon-user"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                v-model="loginForm.password"
                placeholder="请输入密码"
                prefix-icon="iconfont icon-3702mima"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="checked"
                >我已阅读并同意
                <router-link to="" class="red"
                  >《电商后台系统》</router-link
                ></el-checkbox
              >
            </el-form-item>
            <el-form-item class="tar">
              <el-button type="primary" @click="login()">登录</el-button>
              <el-button type="info" @click="resetLofinFrom()">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--底部-->
        <div class="footer">
          <p>
            <span>{{ footerDate.bq }} </span>
            <span>{{ footerDate.icp }}</span>
            <span>{{ footerDate.conpany }}</span>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      checked: true,
      // 双选绑定表单数据
      loginForm: {
        username: "",
        password: "",
      },
      // 验证表单规则
      loginFormRules: {
        // 登录名称验证
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 5,
            max: 12,
            message: "长度在 5 到 12 个字符",
            trigger: "blur",
          },
        ],
        // 登录密码验证
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
      //底部信息
      footerDate: {
        bq: "版权所有2020-2040",
        icp: "皖ICP备********号",
        conpany: "某某信息科技有限公司",
      },
    };
  },
  methods: {
    // 点击重置表单
    resetLofinFrom() {
      console.log(this), this.$refs.loginFormRef.resetFields();
    },
    login() {
      //预验证表单
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) {
          //valid为flase不发请求
          console.log(valid);
          return;
        } else {
          this.$axios
            .post("login", this.loginForm)
            .then((res) => {
              // then用于处于成功事件，res为服务端返回的数据
              let data = res.data;
              if (data.meta.status == 200) {
                // console.log("登录成功");
                console.log(data);
                //登录状态提示
                this.$message.success(data.meta.msg);
                // 将后台获取到的token储存到浏览器sessionStorage中
                window.sessionStorage.setItem("token",data.data.token);
                // 编程式导航跳转到主页main
                this.$router.push("/main")
              } else {
                // console.log("登录失败");
                this.$message.error(data.meta.msg);
                return;
              }
            })
            .catch((error) => {
              console.log(error); // cathch用于处理错误事件
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.background {
  width: 100%;
  height: 100%;
  z-index: -1;
  position: absolute;
}
.bg_login {
  z-index: 1;
  position: relative;
  height: 100%;
  min-height: 500px;
  overflow: auto;
}
.login_content {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 1100px;
  height: 490px;
  margin-left: -550px;
  margin-top: -245px;
}
.header {
  position: absolute;
  left: 0;
  right: 0;
  top: 56px;
  height: 368px;
  background: #1c82eb;
  border-radius: 10px;
  box-shadow: 0 0 20px 2px #0e489e;
}
.login_img {
  position: absolute;
  top: 25px;
  left: 30px;
  width: 500px;
}
.login-header {
  position: absolute;
  top: -85px;
  left: 0;
  text-align: left;
}
.login-wrap {
  position: absolute;
  top: 0;
  right: 65px;
  background-color: #fff;
  padding: 85px 35px 20px;
  width: 300px;
  height: 375px;
  border-bottom-right-radius: 10px;
  border-top-right-radius: 10px;
  margin: 0;
}
.login-wrap:after {
  content: "";
  position: absolute;
  top: 21px;
  z-index: 2;
  left: -54px;
  width: 0;
  height: 0;
  border-right: 40px solid transparent;
  border-left: 40px solid transparent;
  border-top: 40px solid #0b4a9a;
  transform: rotate(-45deg);
}
.login-wrap:before {
  content: "";
  position: absolute;
  bottom: 21px;
  left: -55px;
  width: 0;
  height: 0;
  border-right: 40px solid transparent;
  border-left: 40px solid transparent;
  border-bottom: 40px solid #0b4a9a;
  transform: rotate(45deg);
  z-index: 2;
}
.title_name {
  border-top-right-radius: 10px;
  line-height: 55px;
  background: #1c82eb;
  text-align: center;
  color: #fff;
  font-size: 22px;
  height: 55px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  letter-spacing: 2px;
  z-index: 9;
}
.logo_img {
  width: 100px;
  margin-left: 105px;
  margin-bottom: 30px;
}
/*登录底部*/
.footer {
  position: absolute;
  left: 0;
  bottom: 10px;
  font-size: 14px;
  color: #97bfe8;
}
.footer span {
  margin-right: 15px;
}
</style>
